<?php

namespace App\Api\Controllers;

use App\Api\Requests\ConversionRequest;
use App\Models\Forum;
use App\Models\Follow;
use App\Models\Comment;
use App\Api\Requests\FollowRequest;
use App\Models\Conversion;
use App\Models\UserLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\User;
use Illuminate\Support\Facades\Input;
class ForumController extends Controller
{
//文章
    public function store(forum $forum,Request $request){
        $forum->title = $request->input('title');
        $forum->content = $request->input('content');
        $forum->type = $request->input('type');
        $forum->state = 1;
        $forum->uid = $this->user()->id;
        $forum->sum=0;
        $res = $forum->save();
        return $this->success('成功');
    }

//评论
    public function comment(Request $request, Comment $comment){
        $comment->state = 1;
        $comment->content = $request->input('content');
        $comment->pid = $request->input('pid');
        $comment->uid = $this->user()->id;
        $res = $comment->save();
        return $this->success('成功');
    }
//关注
    public function follow(FollowRequest $followRequest,Follow $follow){
        $uid=$follow->where("uid",$this->user()->id)->where("fid",$followRequest->fid)->first();
        if($uid==""){
            $follow->uid = $this->user()->id;
            $follow->fid = $followRequest->fid;
            if($follow->save()){
                $user =  DB::table('users');
                $sum = $user->where('id', '=', $followRequest->fid)->first();
                $user->where('id', '=', $followRequest->fid)->update(['fans' =>$sum->fans+1 ]);
                $user->where('id', '=', $followRequest->fid)->update(['follow' => $this->user()->follow +1 ]);

                return $this->success('成功');
            }
        }else{
            return $this->success('已关注');

        }
    }


//取消关注
    public function cancel(FollowRequest $followRequest,Follow $follow){
//delete
        $uid=$follow->where("uid",$this->user()->id)->where("fid",$followRequest->fid)->first();
        if($uid!=""){
            $follow->where("uid",$this->user()->id)->where("fid",$followRequest->fid)->delete();
                $user =  DB::table('users');
                $sum = $user->where('id', '=', $followRequest->fid)->first();
                $user->where('id', '=', $followRequest->fid)->update(['follow' =>$sum->fans-1 ]);
                $user->where('id', '=', $followRequest->fid)->update(['follow' =>$this->user()->follow -1 ]);
            return $this->success('成功');
        }else{
            return $this->success('已取消');
        }
    }

    //我的主页
    public function homepage(User $user,forum $forum){
        $user->data=  $user->where("id",$this->user()->id)->get();
        $total_electricity_fee = Forum::where('uid',$this->user()->id)->where('state',1)->get();
        $user->Forum = ($total_electricity_fee);
        return $this->success($user);

    }

    //关注动态
    public function dynamic(Forum $forum){
        $user =  DB::table('follow')->where("uid",81)->get();
        foreach ($user as &$v){
            $data["$v->Id"]=$v->fid;
        }
        return $this->success($forum->wherein('uid',$data)->where('state',1)->get());
    }

    public function recommend(forum $forum,User $user){
       $type=  Input::get('type');
       if ($type==1){
        return $this->success(  $forum->where("recommend",1)->get());
       }else{
           return $this->success(  $user->where("recommend",1)->get());
       }

    }


    public function index(){
        return $this->success($this->user()->forum()->orderBy('id','desc')->get());
    }
}
